#import "CTWebBar.h"
#import "UIImage+WBImage.h"
@interface CTWebBar ()
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, assign) BOOL titleHidden;
@end
@implementation CTWebBar
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _var_backClickSubject = [RACSubject subject];
        _var_closeClickSubject = [RACSubject subject];
        _var_onlineServiceSubject = [RACSubject subject];
        _var_msgCenterClickSubject = [RACSubject subject];
        _var_textBtnClickSubject = [RACSubject subject];
        [self ct_setupSubViews];
    }
    return self;
}
- (void)ct_setupSubViews
{
    _backgroundView = [UIView new];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    _backgroundView.alpha = 1;
    [self addSubview:_backgroundView];
    _var_titleLabel = [UILabel new];
    _var_titleLabel.text = @"";
    _var_titleLabel.textAlignment = NSTextAlignmentCenter;
    _var_titleLabel.textColor = [UIColor whiteColor];
    _var_titleLabel.textAlignment = NSTextAlignmentCenter;
    _var_titleLabel.font = kBoldFontSize(20);
    [self addSubview:_var_titleLabel];
    _backView = [[UIView alloc] init];
    _backView.backgroundColor = [UIColor clearColor];
    _backView.userInteractionEnabled = YES;
    [self addSubview:_backView];
    _var_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _var_backButton.frame = CGRectMake(7, 7, 24, 24);
    UIImage *backImage = [UIImage imageNamed:@"icon_navBack"];
    [_var_backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    _var_backButton.userInteractionEnabled = NO;
    [_backView addSubview:_var_backButton];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    @weakify(self);
    [tap.rac_gestureSignal subscribeNext:^(NSNumber *x) {
        @strongify(self);
        [self.var_backClickSubject sendNext:nil];
    }];
    [_backView addGestureRecognizer:tap];
    _var_closeView = [[UIView alloc] init];
    _var_closeView.backgroundColor = [UIColor clearColor];
    _var_closeView.userInteractionEnabled = YES;
    UIImageView *closeBtn = [[UIImageView alloc] initWithFrame:CGRectMake(7.5,7.5, 15, 15)];
    closeBtn.image = [UIImage imageNamed:@"webview_close"];
    [_var_closeView addSubview:closeBtn];
    [self addSubview:self.var_closeView];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] init];
    [tap1.rac_gestureSignal subscribeNext:^(NSNumber *x) {
        @strongify(self);
        [self.var_closeClickSubject sendNext:nil];
    }];
    [_var_closeView addGestureRecognizer:tap1];
    _var_lineView = [UIView new];
    _var_lineView.backgroundColor = [UIColor colorDDD];
    [self.backgroundView addSubview:_var_lineView];
    [self addSubview:self.var_onlineServiceBtn];
    [self addSubview:self.var_msgCenterBtn];
    [self addSubview:self.var_textBtn];
    _var_onlineServiceBtn.hidden = YES;
    _var_msgCenterBtn.hidden = YES;
    _var_textBtn.hidden = YES;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _backgroundView.frame = self.bounds;
    self.backView.ct_width = 35;
    self.backView.ct_height = 35;
    self.backView.ct_left = 7.5;
    self.backView.ct_bottom = self.ct_height - 3;
    self.var_closeView.ct_width = 35;
    self.var_closeView.ct_height = 35;
    self.var_closeView.ct_left = self.backView.ct_right+7.5;
    self.var_closeView.ct_bottom = self.ct_height - 3;
    [self.var_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.var_closeView.mas_right);
        make.centerY.mas_equalTo(self.var_backButton);
        make.centerX.mas_equalTo(self.backgroundView);
    }];
    [self.var_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-SINGLE_LINE_ADJUST_OFFSET);
        make.height.mas_equalTo(SINGLE_LINE_WIDTH);
    }];
    [self.var_msgCenterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.centerY.mas_equalTo(self.var_backButton);
    }];
    [self.var_onlineServiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.var_msgCenterBtn.mas_left).offset(-8);
        make.size.mas_equalTo(self.var_msgCenterBtn);
        make.centerY.mas_equalTo(self.var_backButton);
    }];
    [self.var_textBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.var_backButton);
    }];
}
- (void)ct_hideCloseBtn
{
    self.var_closeView.hidden = YES;
}
- (void)ct_hiddenTitle
{
    self.titleHidden = YES;
    self.var_titleLabel.hidden = YES;
}
- (void)ct_hideOnlineServiceBtn
{
    self.var_onlineServiceBtn.hidden = YES;
}
- (void)ct_changeBackgroundViewAlpha:(CGFloat)value
{
    if (value>=0.2) {
        [_var_backButton setBackgroundImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
        _var_backButton.ct_size = CGSizeMake(24, 24);
        _var_titleLabel.textColor = [UIColor blackColor];
        _var_titleLabel.hidden = NO;
    }else{
        [_var_backButton setBackgroundImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
        _var_backButton.ct_size = CGSizeMake(24, 24);
        _var_titleLabel.textColor = [UIColor whiteColor];
        _var_titleLabel.hidden = self.titleHidden;
    }
    _backgroundView.alpha = value;
}
#pragma mark lazyloading
- (UIButton *)var_onlineServiceBtn
{
    if (_var_onlineServiceBtn == nil) {
        _var_onlineServiceBtn = [[UIButton alloc] init];
        [_var_onlineServiceBtn setImage:[UIImage imageNamed:@"nav_onlineService"] forState:UIControlStateSelected];
        [_var_onlineServiceBtn setImage:[UIImage imageNamed:@"nav_onlineService"] forState:UIControlStateNormal];
        @weakify(self);
        [[_var_onlineServiceBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self.var_onlineServiceSubject sendNext:nil];
        }];
        _var_onlineServiceBtn.hidden = YES;
    }
    return _var_onlineServiceBtn;
}
- (UIButton *)var_msgCenterBtn {
    if (!_var_msgCenterBtn) {
        _var_msgCenterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _var_msgCenterBtn.frame = CGRectMake(kScreenWidth-50, self.ct_bottom-38, 35, 35);
        [_var_msgCenterBtn setImage:[UIImage imageNamed:@"nav_msg"] forState:(UIControlStateNormal)];
        [_var_msgCenterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _var_msgCenterBtn.hidden = YES;
        @weakify(self);
        [[_var_msgCenterBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self.var_msgCenterClickSubject sendNext:nil];
        }];
    }
    return _var_msgCenterBtn;
}
- (UIButton *)var_textBtn {
    if (!_var_textBtn) {
        _var_textBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_var_textBtn setBackgroundImage:nil forState:UIControlStateNormal];
        [_var_textBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_var_textBtn setTitle:@"" forState:UIControlStateNormal];
        [_var_textBtn setImage:nil forState:UIControlStateNormal];
        [_var_textBtn setImage:nil forState:UIControlStateNormal];
        @weakify(self);
        [[_var_textBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self.var_textBtnClickSubject sendNext:nil];
        }];
    }
    return _var_textBtn;
}
@end
